package civicinity.ist.psu.edu.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.databases.CivicinityDatabase;
import civicinity.ist.psu.edu.rssfeed.RSSItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Lazy Adapter
 * @author kyunghan
 *
 */
public class CivicinityLazyAdapter extends BaseAdapter {

    private Context context;
    private int mLayout;
	private ArrayList<RSSItem> mArr;
    private static LayoutInflater inflater=null;
    public CivicinityImageLoader imageLoader;
    private CivicinityDatabase mDB;
    
	
	public CivicinityLazyAdapter(Context context, int layout, ArrayList<RSSItem> localArray) {
	    context = context;
	    mArr = localArray;
	    
	    mDB = new CivicinityDatabase(context); 
	    
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new CivicinityImageLoader(context.getApplicationContext());
	    // TODO Auto-generated method stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

    public static class ViewHolder{
        public TextView rssTitle;
        public TextView rssPubDate;
        public ImageView rssImage;
        public View thumbnail;
    }
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView == null){
            vi = inflater.inflate(R.layout.rss_item, null);
            holder = new ViewHolder();
            holder.rssTitle = (TextView)vi.findViewById(R.id.rss_title);
            holder.rssPubDate = (TextView)vi.findViewById(R.id.rss_feed_date);
            holder.rssImage = (ImageView)vi.findViewById(R.id.rss_image);
            vi.setTag(holder);
        }
        else
            holder = (ViewHolder)vi.getTag();
        
        // for title
        holder.rssTitle.setText(mArr.get(position).getTitle());
        
        // for pub date
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
        Date resultdate = new Date(Long.parseLong(mArr.get(position).getPubDate()));
        holder.rssPubDate.setText(sdf.format(resultdate));//  + "," + mArr.get(position).getPubDate());
        
        // for image
        holder.rssImage.setTag(Constants.RSS_ICON_URL + mArr.get(position).getRSSURL());
        imageLoader.DisplayImage(Constants.RSS_ICON_URL + mArr.get(position).getRSSURL(), context, holder.rssImage);
        
        return vi;
	}
}
